package com.caijin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caijin.common.constant.UserConstants;
import com.caijin.common.core.domain.Result;
import com.caijin.common.core.domain.entity.Student;
import com.caijin.common.enums.ResultCode;
import com.caijin.system.mapper.StudentMapper;
import com.caijin.system.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 学生信息表 服务实现类
 * </p>
 *
 * @author Peas
 * @since 2021-01-12
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper mapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 学生注册
     *
     * @param student 学生模型
     * @return
     */
    @Override
    public Result register(Student student) {
        if (this.mapper.selectById(student.getSno()) != null) {
            return Result
                    .error()
                    .code(ResultCode.USER_ACCOUNT_ALREADY_EXIST.getCode())
                    .message(ResultCode.USER_ACCOUNT_ALREADY_EXIST.getMessage());
        } else {
            student.setPassword(securityEncryption(student.getPassword()));
            this.mapper.insert(student);
            return Result
                    .ok();
        }
    }

    /**
     * 学生登录
     *
     * @param sno      学号
     * @param password 密码
     * @return
     */
    @Override
    public Result login(HttpServletRequest request, Long sno, String password) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("sno", sno);
        Integer integer = this.mapper.selectCount(wrapper);
        if (integer != 0) {
            Student student = this.mapper.selectOne(wrapper);
            if (passwordEncoder.matches(password, student.getPassword())) {
                HttpSession session = request.getSession();
                session.setAttribute("userId", sno);
                session.setAttribute("userType", UserConstants.STUDENT_TYPE);
                return Result
                        .ok()
                        .message("登录成功");
            } else {
                return Result
                        .error()
                        .message("密码错误,请重新输入");
            }
        } else {
            return Result
                    .error()
                    .message("学号错误,请重新输入");
        }
    }

    /**
     * 修改用户信息
     * 完成度:头像更改
     *
     * @param sno 学号
     * @param avatar 头像
     * @return 结果
     */
    @Override
    public Result modifiedUserInfo(Long sno, String avatar) {
        Student student = this.mapper.selectById(sno);
        student.setAvatar(avatar);
        int i = this.mapper.updateById(student);
        if (i != 0)
        {
            return Result.ok();
        } else
        {
            return Result.error();
        }

    }

    /**
     * 使用spring security自带的密码加密策略
     *
     * @param password 需要加密的密码
     * @return 加密后的密码
     */
    private String securityEncryption(String password) {
        return passwordEncoder.encode(password);
    }
}
